﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Tupa.Controls
{
    [ToolboxData("<{0}:OwCheckBox runat=server ID=OwCheckBox/>")]
    public class OwCheckBox : CheckBox
    {
        /// <summary>
        /// OwCheckBox.
        /// </summary>
        public OwCheckBox()
        {

        }

        [Bindable(true), Category("OwSettings"), Description("CheckBox with nullable value.Automatic change null to false"), Browsable(true), Localizable(true)]
        public new object Checked
        {
            get
            {
                return base.Checked;
            }
            set
            {
                //Check if the value is null or database is null.
                //Set value = false.
                if (value == null || value is DBNull)
                {
                    base.Checked = false;
                    return;
                }
                //If value is not null, then convert to lower and check if it = 1 or = true
                //Set value = true.
                string strChecked = value.ToString().ToLower();
                if (strChecked == "1" || strChecked == "true")
                {
                    base.Checked = true;
                }
                //else, set value = false.
                else
                {
                    base.Checked = false;
                }
            }
        }
    }
}